<?php
class Anchor extends MY_Agent_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $limit = 20;
        $currentPage = (int)$this->input->get_post('page');
        $offset = ($currentPage < 1) ? 0 : ($currentPage - 1) * $limit;


        $where = array('u.recommend_uid'=>$this->uInfo['uid']);

        $this->load->model('agent/Model_agent_anchor', 'anchor');
        $totalNum = $this->anchor->more_count($where);
        $anchorInfo = $this->anchor->more_lists($limit, $offset, $where);

        //分页        
        $this->load->library('pagination');
        $config['total_rows'] = $totalNum;
        $config['per_page'] = $limit;
        $config['base_url'] = '/anchor/index';
        
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();

        $data = array(
            'anchor' => $anchorInfo,
            'page_html' => $pageHtml,
            'total_num' => $totalNum,
        );
        $this->load->view('agent/anchor/index', $data);
    }

    public function create()
    {
        $this->load->view('agent/anchor/create');
    }

    public function save()
    {
        $realname = $this->input->get_post('realname');
        $mobile = $this->input->get_post('mobile');
        $email = $this->input->get_post('email');

        $data = array(
        	'aid' => $this->uInfo['uid'],
            'realname' => $realname,
            'mobile' => $mobile,
            'email' => $email,
            'create_time' => date('Y-m-d H:i:s'),
        );

        $this->load->model('agent/Model_agent_anchor', 'anchor');
        $lastId = $this->anchor->save($data);

        $this->load->helper('url');
        redirect(url('agent').'anchor/index', 'refresh');
    }

    public function delete()
    {

        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $id     = (int)$this->input->get_post('id');
            $status = (int)$this->input->get_post('status');

            if (empty ($id)) {
                $response = error(100000);//参数不全
                break;
            }

            $this->load->model('agent/Model_agent_anchor', 'anchor');
            $item = $this->anchor->getOne(array('id'=>$id, 'aid'=>$this->uInfo['uid']));
            if (empty ($item))
            {
                $response = error(600002);
                break;
            }


            $rs = $this->anchor->save(array('status' => $status), $id);
            
            if (!$rs) {
                $response = error(600003);
                break;
            }

        } while (false);

        $this->$this->json_output($response);
    }
    
    /**
     * 登陆日志
     */
    public function login_log()
    {
    	$page = intval($this->input->get_post('page'));
    	$page = $page ? $page : 1;
    	 
    	$uid = intval($this->input->get_post('uid'));
    	 
    	$where = array();
    	 
    	if($uid){
    		$where['uid'] = $uid;
    	}
    	 
    	$this->load->model('user/Model_user_login_log','login_log');
    	$totalNum = $this->login_log->count($where);
    
    	//分页
    	$this->load->library('pagination');
    	 
    	$config['total_rows'] = $totalNum;
    	$config['per_page'] = 20;
    	$config['base_url'] = '/agent/anchor/log';
    	 
    	$this->pagination->initialize($config);
    	$pageHtml = $this->pagination->create_links();
    
    	$offset = ($page-1) * $config['per_page'];
    	$list = $this->login_log->lists($offset,  $config['per_page'], $where);
    
    	$data = array(
    			'list'   => $list,
    			'pageHtml'   => $pageHtml,
    			'total_num'  => $totalNum,
    	);
    	$this->load->view('agent/anchor/login_log', $data);
    }
    
    public function recommend()
    {
    	$uid = intval($this->input->get_post('uid'));
    	
    	$this->load->model('user/Model_user', 'u');
    	$user_item = $this->u->getOne(array('uid' => $uid));
    	
    	$this->load->view('agent/anchor/recommend', array('user_item'=>$user_item));
    }
    
    public function recommend_save()
    {
    	$uid = $this->input->get_post('uid');
    	$title = $this->input->get_post('title');
    	$content = $this->input->get_post('content');
    	$trailer_start_time = $this->input->get_post('trailer_start_time');
    	$trailer_end_time = $this->input->get_post('trailer_end_time');
    	$live_start_time = $this->input->get_post('live_start_time');
    	$live_end_time = $this->input->get_post('live_end_time');
    	$introduction = $this->input->get_post('introduction');
    	$reason = $this->input->get_post('reason');
    
    	$data = array(
    			'aid' => $this->uInfo['uid'],
    			'uid' => $uid,
    			'title' => $title,
    			'content' => $content,
    			'trailer_start_time' => $trailer_start_time,
    			'trailer_end_time' => $trailer_end_time,
    			'live_start_time' => $live_start_time,
    			'live_end_time' => $live_end_time,
    			'introduction' => $introduction,
    			'reason' => $reason,
    			'create_time' => date('Y-m-d H:i:s'),
    	);
    
    	$this->load->model('agent/Model_agent_anchor_recommend', 'ar');
    	$lastId = $this->ar->save($data);
    
    	$this->load->helper('url');
    	redirect(url('agent').'anchor/index', 'refresh');
    }
}
